package com.sf.rbac.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sf.rbac.domain.Department;
import com.sf.rbac.ex.BusinessException;
import com.sf.rbac.mapper.DepartmentMapper;
import com.sf.rbac.qo.QueryObject;
import com.sf.rbac.service.IDepartmentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;


    @Override
    public void delete(Long id) {
        //判断id是不是假的
        int count = departmentMapper.delete(id);
        //count  是受影响行（row）    , 正常操作成功row=1   假id的话 ,row就不是1
        if (count == 0) {
            throw new RuntimeException("非法操作");
        }
    }

    @Override
    public void saveOrUpdate(Department department) {
        //判断name 和sn不能为空
        if (department.getName() == null) {
            throw new BusinessException("部门名称不能为空");
        }
        if (department.getSn() == null) {
            throw new BusinessException("部门编码不能为空");
        }

        if (department.getId() == null) {
            departmentMapper.save(department);
        } else {
            departmentMapper.update(department);
        }
    }

    @Override
    public Department info(Long id) {
        //判断数据是否为空
        Department department = departmentMapper.get(id);
        //count  是受影响行（row）    , 正常操作成功row=1   假id的话 ,row就不是1
        if (department == null) {
            throw new RuntimeException("非法操作");
        }
        return department;
    }

    @Override
    public List<Department> listAll() {
        return departmentMapper.listAll();
    }

    @Override
    public PageInfo<Department> query(QueryObject queryObject) {
        // 告诉pageNo ,pageSize
        PageHelper.startPage(queryObject.getPageNum(), queryObject.getPageSize());
        //调用selectForList() 查询数据
        List<Department> list = departmentMapper.selectForList(queryObject);
        return new PageInfo<>(list);
    }
}
